﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EquipViewAppNet;
using SecsEquip;

namespace EquipSecs300
{
    public partial class AlarmView : CMyFormEx
    {
        public AlarmView()
        {
          
            InitializeComponent();
            m_pViewName = "警报";

            InitCtrl();          
        }

        //初始化控件
        void InitCtrl()
        {
            listViewAlarm.Columns.Add("ALID", 100, HorizontalAlignment.Left);
            listViewAlarm.Columns.Add("Enable", 100, HorizontalAlignment.Left);
            listViewAlarm.Columns.Add("Set", 200, HorizontalAlignment.Left);
            listViewAlarm.Columns.Add("Description", 200, HorizontalAlignment.Left);
        }

        // 添加数据
        void AddListData(int ALID, bool bEnable, bool bSet, string description)
        {
            listViewAlarm.BeginUpdate();

            string strEnable = bEnable ? "1" : "0";
            string strSet = bSet ? "1" : "0";
            
            ListViewItem lvi = new ListViewItem();
            lvi.Text = ALID.ToString();

            lvi.SubItems.Add(strEnable);
            lvi.SubItems.Add(strSet);
            lvi.SubItems.Add(description);   

            listViewAlarm.Items.Add(lvi);

            listViewAlarm.EndUpdate();
        }

        // 清空数据
        void EmptyListData()
        {
            listViewAlarm.Items.Clear();           
        }

        // 更新数据
        public override void UpdateData()
        {
            // 从工具里面获取
            buttonRefresh_Click(new object(), new EventArgs());
        }

     // 获取当前选择的ID
        private int GetCurSeletID()
        {
            int nALID = 0;
            if (listViewAlarm.SelectedItems.Count != 0)
            {
                string strALID = listViewAlarm.SelectedItems[0].Text;
                try
                {
                    nALID = int.Parse(strALID);
                }
                catch (System.Exception)
                {
                    nALID = 0;
                }
                
            }
            return nALID;
            
        }

        // 获取选中的使能状态
        private bool GetCurSeletEnable()
        {           
            bool bEnable = false;
            if (listViewAlarm.SelectedItems.Count != 0)
            {
                int nID = GetCurSeletID();
                string data = listViewAlarm.SelectedItems[0].SubItems[1].Text;
                bEnable = (data == "1" ? true : false);
            }
            return bEnable;
        }

        // 设置选中的使能状态
        private void SetCurSeletEnable(bool bEnable)
        {
            if (listViewAlarm.SelectedItems.Count != 0)
            {
                int nID = GetCurSeletID();
                string data = bEnable ? "1" : "0";
                listViewAlarm.SelectedItems[0].SubItems[1].Text = data;
            }
        }

        // 获取选中的设置状态
        private bool GetCurSeletSet()
        {
            
            bool bSet = false;
            int nID = GetCurSeletID();
            if (listViewAlarm.SelectedItems.Count != 0)
            {
                string data = listViewAlarm.SelectedItems[0].SubItems[2].Text;
                bSet = (data == "1" ? true : false);               
            }
            return bSet;
        }

        // 设置选中的设置状态
        private void SetCurSeletSet(bool bSet)
        {
            int nID = GetCurSeletID();
            string data = bSet ? "1" : "0";
            if (listViewAlarm.SelectedItems.Count != 0)
            {
                listViewAlarm.SelectedItems[0].SubItems[2].Text = data;
            }
        }


        // 使能
        private void buttonEnable_Click(object sender, EventArgs e)
        {
            if (!GetCurSeletEnable())
            {
                int nALID = GetCurSeletID();
                m_pSecs.AlarmEnable(nALID, true);
                SetCurSeletEnable(true);
            }
        }

        // 去使能
        private void buttonDisable_Click(object sender, EventArgs e)
        {
            if (GetCurSeletEnable())
            {
                int nALID = GetCurSeletID();
                m_pSecs.AlarmEnable(nALID, false);
                SetCurSeletEnable(false);
            }
        }
        // 设置报警
        private void buttonSet_Click(object sender, EventArgs e)
        {
            if (!GetCurSeletSet())
            {
                int nALID = GetCurSeletID();
                m_pSecs.AlarmSet(nALID, true);                
                SetCurSeletSet(true);
            }
        }

        // 清除报警
        private void buttonClear_Click(object sender, EventArgs e)
        {
            if (GetCurSeletSet())
            {
                int nALID = GetCurSeletID();
                m_pSecs.AlarmSet(nALID, false);                   
                SetCurSeletSet(false);
            }
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            int nALID;
            List<AlarmInfo> listInfo = null;
            m_pSecs.AlarmGetList(ref listInfo);

            EmptyListData();
            if (listInfo != null)
            {
                for (int i = 0; i < listInfo.Count; i++)
                {
                    AlarmInfo info = listInfo[i];
                    if (info.ALID != null && info.ALID != "")
                    {
                        try
                        {
                            nALID = int.Parse(info.ALID);
                            AddListData(nALID, info.bEnable, info.bSet, info.ALTX);
                        }
                        catch (System.Exception)
                        {

                        }
                    }                    
                }
            }
           
        }

      
       
    }
}
